package com.ruoyi.charge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.charge.bo.StoreAddBo;
import com.ruoyi.charge.bo.StoreEditBo;
import com.ruoyi.charge.bo.StoreQueryBo;
import com.ruoyi.charge.domain.CommissionRecord;
import com.ruoyi.charge.domain.Store;
import com.ruoyi.charge.mapper.CommissionRecordMapper;
import com.ruoyi.charge.mapper.StoreMapper;
import com.ruoyi.charge.service.IStoreService;
import com.ruoyi.charge.vo.StoreVo;
import com.ruoyi.common.enums.DeletedStatus;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺管理Service业务层处理
 *
 * @author fanqie
 * @date 2021-10-25
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Autowired
    private CommissionRecordMapper commissionRecordMapper;

    @Override
    public StoreVo queryById(Long storeId) {
        Store db = this.baseMapper.selectById(storeId);
        StoreVo storeVo = BeanUtil.toBean(db, StoreVo.class);
        List<CommissionRecord> commissionRecordList = commissionRecordMapper.selectList(
                new LambdaQueryWrapper<CommissionRecord>()
                        .eq(CommissionRecord::getStoreId, storeId)
                        .eq(CommissionRecord::getDeleted, DeletedStatus.VALID.getCode())
        );
        BigDecimal totalCommission = commissionRecordList.stream().map(CommissionRecord::getCommission)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        List<CommissionRecord> dayList = commissionRecordList.stream()
                .filter(e -> e.getCreateTime().after(DateUtils.getCurrentDayDate()))
                .collect(Collectors.toList());
        BigDecimal dayCommission = dayList.stream().map(CommissionRecord::getCommission)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        storeVo.setDayProfit(dayCommission);
        storeVo.setTotalProfit(totalCommission);
        return storeVo;
    }

    @Override
    public List<StoreVo> queryList(StoreQueryBo bo) {
        LambdaQueryWrapper<Store> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getNumber()), Store::getNumber, bo.getNumber());
        lqw.like(StrUtil.isNotBlank(bo.getStoreName()), Store::getStoreName, bo.getStoreName());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), Store::getPhone, bo.getPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getWxNumber()), Store::getWxNumber, bo.getWxNumber());
        lqw.eq(bo.getAgentId() != null, Store::getAgentId, bo.getAgentId());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<StoreVo> entity2Vo(Collection<Store> collection) {
        List<StoreVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, StoreVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<Store> page = (Page<Store>) collection;
            Page<StoreVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(StoreAddBo bo) {
        Store add = BeanUtil.toBean(bo, Store.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(StoreEditBo bo) {
        Store update = BeanUtil.toBean(bo, Store.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Store entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
